package com.itheima.datastructure.binarysearchtree.binarytree;

import java.util.LinkedList;
import java.util.Queue;
/*
思路：
根据层序遍历求最大深度
 */
public class E05Leetcode104_3 {
    public int maxDepth(TreeNode root){
        if(root==null){
            return 0;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        int depth=0;
       // int c1=1;//每层有几个元素
        while(!queue.isEmpty()){
            //int c2=0;
            //jdk写法
            int size=queue.size();
            for(int i=0;i<size;i++){
                TreeNode poll=queue.poll();
                System.out.print(poll.val+"\t");
                if(poll.left!=null){
                    queue.offer(poll.left);
                    //c2++;
                }
                if(poll.right!=null){
                    queue.offer(poll.right);
                    //c2++;
                }
            }
            depth++;
            //c1=c2;
        }
        return depth;
    }
}
